package com.schoolcloud.system.controller;

import com.schoolcloud.common.exception.UserNotExistException;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.system.query.SysUseQuery;
import com.schoolcloud.system.service.SysUserService;
import com.schoolcloud.common.model.R;
import com.schoolcloud.system.entity.SysUser;
import com.schoolcloud.system.vo.SysUserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/system/user")
@RestController
@RequiredArgsConstructor
@Slf4j
public class SysUserController {

    private final SysUserService userService;

    @PostMapping("/login")
    public R<String> login(@RequestBody SysUser user) throws UserNotExistException {
        String userId = userService.selectUserByUserNameAndPassword(user.getUserName(), user.getPassword());
        return R.ok(userId);
    }

    @GetMapping("/info")
    public R<SysUserInfoVO> getSysUserInfo() {
        SysUserInfoVO vo = userService.getSysUserInfo(UserContext.getUser());
        return R.ok(vo);
    }

    @GetMapping("/list")
    public R<List<SysUser>> getSysUserList(SysUseQuery sysUseSearchDTO) {
        List<SysUser> users = userService.getSysUserList(sysUseSearchDTO);
        return R.ok(users);
    }
    @GetMapping("/page")
    public R<PageDTO<SysUser>> getSysUserPage(SysUseQuery sysUseSearchDTO) {
        PageDTO<SysUser> page= userService.getSysUserPage(sysUseSearchDTO);
        return R.ok(page);
    }
    @PostMapping
    public R addSysUser(@RequestBody SysUser sysUser){
        userService.addUser(sysUser);
        return R.ok();
    }

    @PutMapping
    public R modifySysUser(@RequestBody SysUser sysUser){
        userService.modifySysUser(sysUser);
        return R.ok();
    }
}
